package after_eval;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ResultProcesser {

	public static String IN_PATH = "D:/Users/Admin/workspace/Jufo/Data/results_interval_1.txt";
	public static String OUT_PATH = "D:/Users/Admin/workspace/Jufo/Data/parameters.txt";
	
	public static void main(String[] args) {
		try {
			FileReader file = new FileReader(IN_PATH);
	    	BufferedReader in = new BufferedReader(file);
	    	String strLine = "";
	    	int line_counter = 0;
	    	double[] input = new double[4];
	    	int score = 0;
	    	int interval = 0;
	    	double[] parameter = new double[4];
	    	
	    	System.out.println("### Start processing... ###");
	    	
	    	strLine = in.readLine();
	    	while (strLine != null) {
	    		line_counter++;


    			//read values from file
    			Scanner scan = new Scanner(strLine).useDelimiter("\t");
    			for (int i = 0; i < 4; i++) {
    				input[i] = Double.parseDouble(scan.next());
    			}
    			score = Integer.parseInt(scan.next());
    			interval = Integer.parseInt(scan.next());
    			
//	    			if (score <= 10) {
//		    			input = normParameter(input);
//		    			for (int i = 0; i < 4; i++) {;
//		    				parameter[i] = parameter[i] + (input[i]/(Math.log(score)+1));
////		    				System.out.println(parameter[i] + " @ " + (input[i]/(Math.log(score)+1)));
//		    			}
//	    			}
    			
    			if (interval != 0) {
    				input = normParameter(input);
        			for (int i = 0; i < 4; i++) {
        				parameter[i] += (input[i]*Math.log(interval+1));
        			}
    	    		System.out.println(strLine + "\t " + line_counter);
    			}

	    		if (line_counter % 10000 == 0) {
	    			System.out.println("Sentence " + line_counter);
	    		}
	    		
	    		strLine = in.readLine();
	    	}
	    	
	    	System.out.println("### Processing finished ###");
	    	
	    	//output parameters
	    	String output = "";
	    	parameter = normParameter(parameter);
	    	for (int i = 0; i < 4; i++) {
	    		parameter[i] = parameter[i]/((double)line_counter);
	    	}
	    	parameter = normParameter(parameter);
	    	for (int i = 0; i < 4; i++) {
	    		output += parameter[i] + "\t";
	    	}
	    	System.out.println("Parameter: "+ output + "\n");
	    	writeResult(output);
	    	
		}catch(IOException e) {
			e.printStackTrace();
		}

	}
	
	//norm Parameters: highest Parameter = 1.0
	private static double[] normParameter(double[] unnormed) {
		double highestValue = 0;
		for (int i = 0; i < 4; i++) {
			if (unnormed[i] > highestValue) {
				highestValue = unnormed[i];
			}
		}
		for (int i = 0; i < 4; i++) {
			unnormed[i] = unnormed[i]/highestValue;
		}
		return unnormed;

	}
	
	//write result to file
	private static void writeResult(String output) {
		try {
			File file = new File(OUT_PATH);
			FileWriter out = new FileWriter(file, true);

			out.write(output);

			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
